<?php
/**
 * 商品列表控制器
 * 
 * @version $Id: Zhe.php 674 2013-02-05 14:37:24Z linsir123 $
 */
class controllerZhe extends TaobaokeController 
{
	/**
	 * @var 模型
	 */
	public $mItem;
	
	/**
	 * 构造函数
	 */
	public function __construct()
	{
		parent::__construct();
		
		///
		$this->mItem = M::item();
		
		///
		$this->listType = 1;
		$this->taobaoke = array('show'=>15);
		$this->sorts = array(
			array('默认', 'default'),
			array('折扣', 'price_asc'),
			array('信用', 'credit_desc'),
			array('销量', 'volume_desc'),
		);
		
		///
		$this->_init();
		$this->_checkCompatible();
		
		/**
		 * 缓存重建
		View::assign('dh', App::O('zhe', 'class'));
		 */
	}
	
	/**
	 * 淘宝推广页
	 */
	public function actionIndex()
	{
		///
		$data = $this->mItem->getTaobaokesByCoupon($this->taobaoke, true);
		list($list, $totalPage, $total) = $data;
		
		///
		AppView::addCss('themes/01/zhe.css');
		View::display('zhe/index');
		View::assignSet(array(
			'total' => $total, 
			'list' => $list, 
			'sortInfo' => $this->_aSortInfo(), 
			'pagination' => $this->_aPagination($totalPage), 
		));
		$this->_assignPage();
		$this->_assignTopic();
	}
	
	
	/// 以下为私有方法 ///
	
	
	/**
	 * 初始化
	 */
	private function _init()
	{
		$keys = array(
			'pinyin_keyword' => 'trim',
			'keyword' => 'trim',
			'sort' => 'intval',
			'p' => 'intval',
		);
		$keysTaobaoke = array('keyword', 'sort', 'p');
		foreach ($keys as $k => $m) {
			$v = call_user_func_array($m, array($_GET[$k]));
			if ( ! empty($v)) {
				$this->data[$k] = $v;
				if (in_array($k, $keysTaobaoke)) {
					if ($k == 'sort') 
						$v = $this->sorts[$v][1];
						
					$this->taobaoke[$k] = $v;
				}
			}
		}
		
		///
		if ( ! empty($this->data['pinyin_keyword'])) {
			$v = $this->mTerm->get($this->data['pinyin_keyword']);
			if ( ! empty($v)) {
				$this->data['keyword'] = $this->taobaoke['keyword'] = $v;
				$this->cleanKeyword = true;
			}
		}
	}
	
	/**
	 * 兼容性
	 */
	private function _checkCompatible()
	{
		$keyword = $this->data['keyword'];
		$pinyinKeyword = $this->data['pinyin_keyword'];
		
		/// 
		$params = array();
		if ($this->data['sort'] >= count($this->sorts)) 
			$params['sort'] = 0;
			
		/// 1 关键字
		if (empty($keyword)) {
			$params['pinyin_keyword'] = 'nvzhuang';
			$this->_h301($params);
		}
		
		/// 2 关键字
		if ($this->cleanKeyword && ! empty($_GET['keyword'])) {
			$params['keyword'] = 0;
			$this->_h301($params);
		}
		
		/// 3 关键字
		if ( ! empty($keyword)) {
			App::V('pinyin');
			$tmp = string2pinyin($keyword);
			if ($tmp != $pinyinKeyword) {
				$params['pinyin_keyword'] = $tmp;
				$this->_h301($params);
			}
		}
		
		/// 4 其它
		if ( ! empty($params)) 
			$this->_h301($params);
	}
	
	/**
	 * 转换到视图层
	 */
	private function _assignPage()
	{
		$page = array('title'=>'','keywords'=>'','description'=>'');
		$keyword = $this->data['keyword'];
		
		/// title
		$page['title'] = $keyword;
		
		if ($this->data['p'] > 1) 
			$page['title'] .= '(第'. $this->data['p'] .'页)';
			
		$page['title'] .= sprintf(",淘宝网%s,淘宝网%s限时折扣,淘宝网%s打折专区", 
			$keyword, $keyword, $keyword);
			
		/// keywords
		$page['keywords'] = "淘宝网,淘宝商城,天猫商城,". $keyword .",限时折扣,折扣网,淘宝打折,淘宝打折专区";
		
		/// description
		$page['description'] = "www.e35go.com为您推荐淘宝". $keyword ."限时折扣促销,精心为您挑选淘宝每日限时秒杀促销商品,收藏本站方便您下次访问!";
		
		///
		View::assignSet(array(
			'page' => $page, 
			'keyword' => empty($this->data['keyword'])?"":$this->data['keyword'], 
		));
	}
	
	/**
	 * 转换到视图层
	 */
	private function _assignTopic()
	{
		$keyword = $this->data['keyword'];
		
		///
		$topics = M::topic()->getList($keyword);
		View::assign('topics', $topics);
	}
}
